package com.linkage.jz.bankruptcy.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.bean.ZQUserInfo;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;
import cn.bingoogolapple.swipeitemlayout.BGASwipeItemLayout;
import jp.wasabeef.glide.transformations.CropCircleTransformation;


public class ProZQUserSelListViewAdapter extends BGARecyclerViewAdapter<ZQUserInfo> {
    /**
     * 当前处于打开状态的item
     */
    private List<BGASwipeItemLayout> mOpenedSil = new ArrayList<>();
    private Context mContext;

    public ProZQUserSelListViewAdapter(RecyclerView recyclerView, Context context) {

        super(recyclerView, R.layout.item_zqlist);
        mContext= context;
    }

    @Override
    public void setItemChildListener(BGAViewHolderHelper viewHolderHelper, int viewType) {
        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
        swipeItemLayout.setDelegate(new BGASwipeItemLayout.BGASwipeItemLayoutDelegate() {
            @Override
            public void onBGASwipeItemLayoutOpened(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
                mOpenedSil.add(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutClosed(BGASwipeItemLayout swipeItemLayout) {
                mOpenedSil.remove(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutStartOpen(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
            }
        });
        viewHolderHelper.setItemChildClickListener(R.id.tv_item_bgaswipe_delete);
        viewHolderHelper.setItemChildClickListener(R.id.tv_item_bgaswipe_edit);
        viewHolderHelper.setItemChildClickListener(R.id.iv_manageuser_add);

        viewHolderHelper.setItemChildLongClickListener(R.id.tv_item_bgaswipe_delete);
    }

    @Override
    public void fillData(BGAViewHolderHelper viewHolderHelper, int position, ZQUserInfo userInfo) {
//        viewHolderHelper.setText(R.id.tv_item_bgaswipe_title, "111")
//                .setText(R.id.tv_item_bgaswipe_detail,"22");

        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
//        if ( userInfo.getCurType()== 1) {
        viewHolderHelper.setVisibility(R.id.test, View.GONE);
        viewHolderHelper.setVisibility(R.id.test2, View.VISIBLE);
        viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_edit, View.GONE);
        viewHolderHelper.setText(R.id.tv_item_bgaswipe_title1, userInfo.getCreditorName());
//            viewHolderHelper.setText(R.id.tv_item_bgaswipe_title1, userInfo.getUser().getLoginname());
//            viewHolderHelper.setText(R.id.tv_item_bgaswipe_title2, userInfo.getUser().getCompany());

        DecimalFormat decimalFormat = new DecimalFormat("0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
        float f = 0.00f;
        if(userInfo.getAmountDeclared()!=null && !"".equalsIgnoreCase(userInfo.getAmountDeclared())) {
            f = Float.parseFloat(userInfo.getAmountDeclared());
        }

        String p = decimalFormat.format(f);
        viewHolderHelper.setText(R.id.tv_item_bgaswipe_title2, "申报金额：" + p + "元");
        if(userInfo.getConfirmAmount()!=null && !"".equalsIgnoreCase(userInfo.getConfirmAmount())) {
            f = Float.parseFloat(userInfo.getConfirmAmount());
        }
        String p1 = decimalFormat.format(f);
        if (!p.equalsIgnoreCase(p1)) {
            viewHolderHelper.setTextColorRes(R.id.tv_item_bgaswipe_title4, R.color.page_top);
        }
        viewHolderHelper.setText(R.id.tv_item_bgaswipe_title4, p1);

        if (userInfo.getMessageCount() != null && "0".equalsIgnoreCase(userInfo.getMessageCount())) {
//            viewHolderHelper.setVisibility(R.id.img_red, View.VISIBLE);
            viewHolderHelper.setVisibility(R.id.img_red2, View.VISIBLE);
            viewHolderHelper.setText(R.id.img_red2, "  " + userInfo.getMessageCount() + "  ");
        } else {
//            viewHolderHelper.setVisibility(R.id.img_red, View.GONE);
            viewHolderHelper.setVisibility(R.id.img_red2, View.GONE);
        }

        if(userInfo.getCommitteeChairman()!=null && userInfo.getCommitteeChairman().equalsIgnoreCase("true"))
        {
            viewHolderHelper.setVisibility(R.id.tv_china, View.VISIBLE);
        }else{
            viewHolderHelper.setVisibility(R.id.tv_china, View.GONE);
        }

        viewHolderHelper.setVisibility(R.id.tv_tag1, View.GONE);
        viewHolderHelper.setVisibility(R.id.tv_tag2, View.GONE);
        viewHolderHelper.setVisibility(R.id.tv_tag3, View.GONE);

        ImageView imageview = viewHolderHelper.getView(R.id.iv_item_bgaswipe_avatar1);
        Glide.with(mContext).load(userInfo.getAvatar())
                .bitmapTransform(new CropCircleTransformation(mContext))
                .placeholder(R.drawable.logo)
                .into(imageview);

        if ("2".equalsIgnoreCase(userInfo.getOnlineStatus())) {
            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_online, View.VISIBLE);
        } else {
            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_online, View.INVISIBLE);
        }
        if ("true".equalsIgnoreCase(userInfo.getKindGuarantee())) {
            viewHolderHelper.setText(R.id.tv_tag1, "担保优先");
            viewHolderHelper.setVisibility(R.id.tv_tag1, View.VISIBLE);
            viewHolderHelper.setBackgroundRes(R.id.tv_tag1, R.drawable.bg_text_pro_talk_member_tag_dbyx);

        }
        if ("true".equalsIgnoreCase(userInfo.getKindTax())) {
            viewHolderHelper.setText(R.id.tv_tag1, "税务");
            viewHolderHelper.setVisibility(R.id.tv_tag1, View.VISIBLE);
            viewHolderHelper.setBackgroundRes(R.id.tv_tag1, R.drawable.bg_text_pro_talk_member_tag_sw);
        }
        if ("true".equalsIgnoreCase(userInfo.getKindOther())) {
            viewHolderHelper.setText(R.id.tv_tag2, "其他优先");
            viewHolderHelper.setVisibility(R.id.tv_tag2, View.VISIBLE);
            viewHolderHelper.setBackgroundRes(R.id.tv_tag2, R.drawable.bg_text_pro_talk_member_tag_qtyx);

        }
        if ("true".equalsIgnoreCase(userInfo.getKindOrdinary())) {
            viewHolderHelper.setText(R.id.tv_tag3, "普通");
            viewHolderHelper.setVisibility(R.id.tv_tag3, View.VISIBLE);
            viewHolderHelper.setBackgroundRes(R.id.tv_tag3, R.drawable.bg_text_pro_talk_member_tag_pt);
        }
        if (userInfo.getContact() == null || "".equalsIgnoreCase(userInfo.getContact())) {
            viewHolderHelper.setVisibility(R.id.iv_email, View.INVISIBLE);
            viewHolderHelper.setVisibility(R.id.iv_phone, View.INVISIBLE);

        } else {
            viewHolderHelper.setVisibility(R.id.iv_email, View.VISIBLE);
            viewHolderHelper.setVisibility(R.id.iv_phone, View.VISIBLE);
            if ("2".equalsIgnoreCase(userInfo.getOnlineStatus())) {
                viewHolderHelper.setVisibility(R.id.iv_email, View.INVISIBLE);
            }
        }
        viewHolderHelper.setItemChildClickListener(R.id.iv_phone);
        viewHolderHelper.setItemChildClickListener(R.id.iv_email);

//            else {
//
//            }


//        }
//            swipeItemLayout.setSwipeAble(false);
//        } else {
            swipeItemLayout.setSwipeAble(false);
//        }
    }

    public void closeOpenedSwipeItemLayoutWithAnim() {
        for (BGASwipeItemLayout sil : mOpenedSil) {
            sil.closeWithAnim();
        }
        mOpenedSil.clear();
    }
}